package com.chen.pspms.controller;

import com.chen.pspms.entity.User;
import com.chen.pspms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

@Controller
@RequestMapping("/user")
@SessionAttributes("user")
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/login")
    public String login(Model model){
        //跳转到登录界面
        return "登录/login";
    }

    @RequestMapping("/authenticate")
    public String authenticate(User user,Model model){
        //跳转到登录界面
        User user1= userService.findyByAccount(user.getAccount());
        if(user1==null || !(user1.getPasswd().equals(user.getPasswd()))){
            //账号密码不正确
            String exist="账号或密码错误";
            model.addAttribute("exist",exist);
            return "登录/login";
        }
        String role = user1.getRole();
        switch(role){
            case "管理员": return "管理员首页";
            case "企业" : return "企业工作首页";
            case "政府" : return "政府工作首页";
            case "巡检" : return "巡检工作首页";
        }
        return null;
    }

    @RequestMapping("/register")
    public String register(Model model){
        //跳转到登录界面
        return "登录/register";
    }

    @RequestMapping("/save")
    public String save(User user, Model model){
        //根据账号密码查找
        User user1 = userService.findyByAccount(user.getAccount());
        if(user1!=null){
            //账号密码相同
            String exist="账号已存在";
            model.addAttribute("exist",exist);
            return "登录/register";
        }else{
            userService.save(user);
        }
        return "登录/login";
    }

}
